package com.zyq.controller.manager;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyq.exception.CustomException;
import com.zyq.model.common.BaseDto;
import com.zyq.model.common.ResponseBean;
import com.zyq.model.entity.User;
import com.zyq.model.request.UserDto;
import com.zyq.model.response.UserStatusResponse;
import com.zyq.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员特有接口
 * @author: zyq
 * @create:
 * 2021-12-01
 **/
@Api( tags = "管理员特有接口")
@RestController
@RequestMapping("/manager")
public class managerController {


    @Autowired
    private IUserService userService;

    /**
     * 管理员审核用户注册信息
     * @return
     */
    @RequiresRoles("管理员")
    @ApiOperation("获取审核用户列表")
    @RequestMapping(value = "/selectUnAuthorizedUser", method = RequestMethod.GET)
    public ResponseBean getUnAuthorizedUserList(Long status, BaseDto baseDto,@RequestHeader String Authorization) {
        if (baseDto.getPageNum() == null || baseDto.getPageSize() == null) {
            baseDto.setPageNum(1);
            baseDto.setPageSize(10);
        }
        PageHelper.startPage(baseDto.getPageNum(), baseDto.getPageSize());
        List<UserStatusResponse> userStatusResponsesList = userService.getUnAuthorizedUserList(status);
        PageInfo<UserStatusResponse> selectPage = new PageInfo<UserStatusResponse>(userStatusResponsesList);
        if (userStatusResponsesList == null || userStatusResponsesList.size() < 0) {
            throw new CustomException("查询失败(Query Failure)");
        }
        for(int i = 0 ; i < userStatusResponsesList.size() ; i++){
            if(userStatusResponsesList.get(i).getStatus() == 0) {
                userStatusResponsesList.get(i).setStatusDetail("未审核");
            }else if(userStatusResponsesList.get(i).getStatus() == 1 ){
                userStatusResponsesList.get(i).setStatusDetail("审核通过");
            }else{
                userStatusResponsesList.get(i).setStatusDetail("审核未通过");
            }
        }
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("total", selectPage.getTotal());
        result.put("pages", selectPage.getPages());
        result.put("data", userStatusResponsesList);
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }

    /**
     * 审核用户
     * @return
     */
    @RequiresRoles("管理员")
    @ApiOperation("审核用户")
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    public ResponseBean updateStatus(User user,@RequestHeader String Authorization) {
        userService.updateStatus(user);
        return new ResponseBean(HttpStatus.OK.value(), "操作成功",null);
    }


}
